/**********
@name: depart科室选择组件
@describtion:
@author:liubingye
@date:2023-03-03 11:18:10
***********/
<template>
  <div class="depart-outer-box">
    <el-form-item :label="props.title" :rules="departRule" :prop="props.field">
      <el-popover
          :visible="popoverVisible"
          trigger="focus"
          :width="700"
      >
        <div class="inner-depart-option">
          <div v-if="level === 2">
            <el-tabs v-model="activeName" @tab-change="handleTabChange">
              <el-tab-pane v-for="item in props.options" :label="item.name" :key="item.id" :name="item.id">
                <el-scrollbar height="300px">
                  <div class="depart-wrap" v-for="(column,key) in item.children" :key="key">
                    <h4 class="depart-class-title">{{key}}</h4>
                    <div class="depart-class-list">
                      <span class="depart" :class="{'is-chose': judgeChose(depart)}" v-for="depart in column" :key="depart.id" @click="handleChooseDepart(depart)">{{depart.name}}</span>
                    </div>
                  </div>
                </el-scrollbar>
              </el-tab-pane>
            </el-tabs>
          </div>
          <div v-if="level === 3">
            <el-tabs v-model="activeName" @tab-change="handleTabChange">
              <el-tab-pane v-for="item in props.options" :label="item.name" :key="item.id" :name="item.id">
                <el-tabs tab-position="left" @tab-change="handleTabChange">
                  <el-tab-pane v-for="columnClass in item.children" :key="columnClass.id" :label="columnClass.name">
                    <el-scrollbar height="300px">
                      <div class="depart-wrap" v-for="(column,key) in columnClass.children" :key="key">
                        <h4 class="depart-class-title">{{key}}</h4>
                        <div class="depart-class-list">
                          <span class="depart" :class="{'is-chose': judgeChose(depart)}" v-for="depart in column" :key="depart.id" @click="handleChooseDepart(depart)">{{depart.name}}</span>
                        </div>
                      </div>
                    </el-scrollbar>
                  </el-tab-pane>
                </el-tabs>
              </el-tab-pane>
            </el-tabs>
          </div>
        </div>
        <template #reference>
          <div ref="popReference" :class="{'is-focus': selectFocus}" class="depart-select" tabindex="-1" @focus="handleFocusDepart" @blur="handleBlurDepart">
            <div class="depart-container">
          <span class="chose-depart" v-for="item in choseDepartList" :key="item.id">
            {{item.name}}
            <el-icon @click="handleCancel(item)"><Close /></el-icon>
          </span>
            </div>
            <div class="choose-depart">
              <span>选择{{props.title}}</span>
              <i class="iconfont">&#xe678;</i>
            </div>
          </div>
        </template>
      </el-popover>
    </el-form-item>
  </div>
</template>

<script setup>
import { ref, onMounted, computed } from "vue";
import { Close } from '@element-plus/icons-vue'
const props = defineProps({
  title: {
    type: String,
    default: ''
  },
  value: {
    type: Array,
    default: null
  },
  options: {
    type: Array,
    default: null
  },
  field: {
    type: String,
    default: ''
  },
  required: {
    type: Boolean,
    default: false
  },
  level: {
    type: Number,
    default: 2
  }
})
const popReference = ref()
const popoverVisible = ref(false)
const choseDepartList = computed({
  get: () => props.value ? props.value : [],
  set: (val) => {
    emits('update:value', val)
  }
})
const emits = defineEmits(['update:value'])

const level = ref(props.level)
const activeName = ref(props.options[0].id)
const selectFocus = ref(false)

const handleFocusDepart = () => {
  selectFocus.value = true
  popoverVisible.value = true
}
const handleTabChange = () => {
  popReference.value.focus()
}
const handleBlurDepart = () => {
  selectFocus.value = false
  popoverVisible.value = false
}
const handleChooseDepart = (depart) => {
  popReference.value.focus()
  if(choseDepartList.value.some((item) => {
    return item.id === depart.id
  })){
    choseDepartList.value = choseDepartList.value.filter(item => {
      return item.id !== depart.id
    })
  }else{
    choseDepartList.value.push(depart)
  }
}
const judgeChose = (depart) => {
  return choseDepartList.value.some((item) => {return item.id === depart.id})
}
const handleCancel = (depart) => {
  popoverVisible.value = false
  popReference.value.blur()
  choseDepartList.value = choseDepartList.value.filter(item => {return item.id !== depart.id})
}
const departRule = ref([])
const validateDepart = (rule, value, callback) => {
  if (!value || value.length === 0) {
    callback(new Error(props.title + '不能为空'))
  } else {
    callback()
  }
}
onMounted(() => {
  if(props.required){
    departRule.value.push({validator: validateDepart,trigger: 'blur'})
  }
})
</script>

<style scoped lang="scss">
.depart-outer-box{
  min-height: 56px;
  box-sizing: border-box;
  border-bottom: 1px solid #E4E4E4;
  display: flex;
  align-items: center;
  ::v-deep(.el-form-item){
    align-items: center;
    margin-bottom: 0;
    width: 100%;
  }
}
.depart-select{
  min-height: 42px;
  width: 100%;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-sizing: border-box;
  padding: 0 10px;
  .choose-depart{
    display: flex;
    flex-shrink: 0;
    span{
      color: $mainColor;
    }
    i{
      margin-left: 10px;
      font-size: 16px;
      color: #CACED4;
    }
  }
  .depart-container{
    display: flex;
    flex-wrap: wrap;
    min-height: 40px;
    .chose-depart{
      display: flex;
      align-items: center;
      padding: 0 5px;
      height: 20px;
      line-height: 20px;
      margin: 10px;
      border-radius: 5px;
      background-color: #f0f2f5;
      cursor: default;
      i{
        cursor: pointer;
        margin-left: 10px;
      }
    }
  }
}
.depart-wrap{
  display: flex;
  .depart-class-title{
    color: $mainColor;
    width: 20px;
    flex-shrink: 0;
  }
  .depart-class-list{
    display: flex;
    flex-wrap: wrap;
    span.depart{
      display: block;
      padding: 0 5px;
      height: 20px;
      line-height: 21px;
      margin: 0 10px 10px 0;
      border-radius: 5px;
      cursor: pointer;
      &:hover{
        background-color: $borderColor;
      }
    }
    span.depart.is-chose{
      background-color: $mainColor!important;
      color: $white;
    }
  }
}
::v-deep(.el-tabs .el-tabs__item){
  text-overflow: ellipsis;
  white-space: normal;
  -webkit-line-clamp: 1;
  max-width: 160px;
}
</style>
